package com.cloudstudy.tool;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.lang3.StringUtils;

import java.io.IOException;
import java.text.SimpleDateFormat;

/**
 *
 * @author Tony
 * @since 20210130
 */
public class JsonTool {

    public static String toJson(Object target) {
        return toJson(target, null);
    }

    /**
     * @param dateFormat "yyyy-MM-dd HH:mm:ss"
     * @return string
     */
    public static String toJson(Object target,String dateFormat) {
        return toJson(target, dateFormat,true);
    }

    /**
     * @param dateFormat "yyyy-MM-dd HH:mm:ss"
     * @return string
     */
	public static String toJson(Object target,String dateFormat,boolean includeNotNull) {
		ObjectMapper mapper = new ObjectMapper();
		if (!StringUtils.isEmpty(dateFormat))
		    mapper.setDateFormat(new SimpleDateFormat(dateFormat));

		if (includeNotNull){
            mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
        }
		try {
			return mapper.writeValueAsString(target);
		} catch (JsonProcessingException e) {
			LogTool.error(JsonTool.class, e);
		} catch (IOException e) {
            LogTool.error(JsonTool.class, e);
		}
		return null;
	}

	@SuppressWarnings("unchecked")
	public static <T> T toObject(String json, Class<?> clazz) {
		return toObject(json, clazz, null);
	}

	/**
	 * @param json
	 * @param clazz
	 * @param dateFormat "yyyy-MM-dd HH:mm:ss"
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static <T> T toObject(String json, Class<?> clazz,String dateFormat) {
        if (StringUtils.isEmpty(json)){
            return null;
        }

	    ObjectMapper mapper = new ObjectMapper();
        mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        if (!StringUtils.isEmpty(dateFormat)) {
            mapper.setDateFormat(new SimpleDateFormat(dateFormat));
        }
        try {
			return (T) mapper.readValue(json, clazz);
		} catch (Exception e) {
            LogTool.error(JsonTool.class, e);
		}
		return null;
	}

	public static JsonNode readValue(String json, String nodeName) throws JsonProcessingException, IOException{
		ObjectMapper objectMapper = new ObjectMapper();
		JsonNode rootNode = objectMapper.readTree(json);
		return rootNode.path(nodeName);
	}
//
//	public static void main(String[] args) throws Exception {
//        Employee.EmployeeBuilder builder = new Employee.EmployeeBuilder();
//        builder.id(100).name("张三");
//        System.out.println("=== builder ===");
//        System.out.println(builder.toString());
//
//        System.out.println("=== test01 ===");
//        String json = JsonTool.toJson(builder.build());
//        System.out.println(json);
//
//        System.out.println("=== test02 ===");
//        json = JsonTool.toJson(builder.build(), null,false);
//        System.out.println(json);
//
//        System.out.println("=== test03 ===");
//        builder.birth(DtTool.convertStringToDate(DtTool.DATE_MASK,"2010-10-05"));
//        json = JsonTool.toJson(builder.build(), DtTool.DATE_MASK,true);// yyyy-MM-dd HH:mm:ss
//        System.out.println(json);
//
//	}
}

//@Builder
//@Data
//@AllArgsConstructor
//@NoArgsConstructor
//class Employee{
//    private long id;
//    private String name;
//    private String address;
//    private Date birth;
//}